#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) Huoty, All rights reserved
# Author: Huoty <sudohuoty@163.com>
# CreateTime: 2020-06-09 10:36:22


class Node(object):

    def __init__(self, data, next=None):
        self.data = data
        self.next = next


class SingleLink(object):
    """单链表"""

    def __init__(self):
        self.head = None  # 头指针

    @property
    def length(self):
        """链表长度"""
        probe = self.head
        count = 0
        while probe is not None:
            count += 1
            probe = probe.next
        return count

    def is_empty(self):
        """判断链表是否为空"""
        return self.head is None

    def add(self, data):
        """往头部添加数据"""
        node = Node(data)
        node.next = self.head
        self.head = node

    def append(self, data):
        """往尾部添加数据"""
        node = Node(data)
        if self.is_empty():
            self.head = node
        else:
            probe = self.head
            while probe.next is not None:
                probe = probe.next
            probe.next = node

    def reverse(self):
        new_link = SingleLink()
        probe = self.head
        while probe is not None:
            new_link.add(probe.data)
            probe = probe.next
        return new_link

    def travel(self):
        probe = self.head
        while probe is not None:
            yield probe.data
            probe = probe.next


sl = SingleLink()
for n in range(1, 11):
    sl.append(n)
sl_len = sl.length
sl2 = sl.reverse()
new_link = SingleLink()
probe = sl.head
probe2 = sl2.head
for _ in range(sl_len // 2):
    new_link.append(probe.data)
    new_link.append(probe2.data)
    probe = probe.next
    probe2 = probe2.next
if sl_len % 2 == 1:
    new_link.append(probe.data)

print(list(new_link.travel()))
